package com.abc.robot01.activity



import android.os.Bundle
import androidx.compose.animation.core.animateDpAsState
import androidx.compose.animation.core.tween
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.interaction.MutableInteractionSource
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.offset
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.text.BasicText
import androidx.compose.material3.Surface
import androidx.compose.runtime.Composable
import androidx.compose.runtime.MutableIntState
import androidx.compose.runtime.MutableState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableIntStateOf
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.layout.onGloballyPositioned
import androidx.compose.ui.layout.positionInParent
import androidx.compose.ui.platform.ComposeView
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import androidx.fragment.app.Fragment
import com.abc.robot01.R
import com.abc.robot01.databinding.ContentMain4Binding
import com.abc.robot01.fragment.CustomersFragment
import com.abc.robot01.fragment.QAFragment
import com.abc.robot01.fragment.RobotInfoFragment
import com.abc.robot01.fragment.RobotInfoFragment7
import com.abc.robot01.fragment.SystemMessageFragment2
import com.abc.robot01.fragment.WalletFragment
import com.abc.robot01.view.enableFaClick
import com.abc.robot01.view.enableHomeClick
import com.abc.robot01.view.enableInfoClick
import com.abc.robot01.view.enableSpClick
import kotlin.math.roundToInt


open class SysMessageActivity : BaseVoiceActivity() {

     lateinit var binding: ContentMain4Binding

    override fun handleVoiceCommand(asr: String): Boolean {
        if(asr.contains("商品")) {
            startActivity<ShangPinKuActivity> { }
            playTTS("好的")
            finish()
            return true
        } else
            if(asr.contains("信息")) {
                return true
            }
            else
                if(asr.contains("方案")) {
                    startActivity<FangAnKuActivity> { }
                    playTTS("好的")
                    finish()
                    return true
                }
        if(asr.contains("首页")) {
            finish()
            playTTS("好的")
            return true
        }
        return false
    }

    @Composable
    fun  TabTitle(
        showLeft: MutableState<Boolean>,
        showRight: MutableState<Boolean>,
        centerX: MutableIntState,
        largeX: MutableIntState,
        currentTab: MutableState<String>
    ) {
        var position1 by remember { mutableIntStateOf(0) }
        var position2 by remember { mutableIntStateOf(0) }
        var position3 by remember { mutableIntStateOf(0) }
        var position4 by remember { mutableIntStateOf(0) }
        var position5 by remember { mutableIntStateOf(0) }
        var position6 by remember { mutableIntStateOf(0) }

        Row(modifier = Modifier
            .fillMaxWidth()
            .padding(start = 50.px)
            .offset(0.dp, 0.dp),horizontalArrangement = Arrangement.spacedBy(83.px),
        ) {
            BasicText(
                text = "系统信息",
                style = TextStyle(
                            fontWeight = FontWeight.Bold,
                    fontSize = size,
                    color = if(currentTab.value=="系统信息") MYBLACK else MYBLACK_Percent60 ,
                ),
                modifier = Modifier
                    .clickable(
                        indication = null,  // Removes the ripple effect
                        interactionSource = remember { MutableInteractionSource() }  // Keeps track of interactions
                    ) {
                        showLeft.value = true;
                        showRight.value = false;
                        centerX.intValue = 0
                        largeX.intValue= 0
                        currentTab.value = "系统信息"
                    }
            )

            BasicText(
                text = "机器人信息",
                style = TextStyle(
                            fontWeight = FontWeight.Bold,
                    fontSize = size,
                    color = if(currentTab.value=="机器人信息") MYBLACK else MYBLACK_Percent60 ,
                ),
                modifier = Modifier
                    .clickable(
                        indication = null,  // Removes the ripple effect
                        interactionSource = remember { MutableInteractionSource() }  // Keeps track of interactions
                    ) {
                        showLeft.value = false;
                        showRight.value = false;
                        centerX.intValue = position1
                        largeX.intValue= 0
                        currentTab.value = "机器人信息"
                    }
                    .onGloballyPositioned {
                        position1 = (it.positionInParent().x +  it.size.width/2).roundToInt()
//                        println("Font size is position1:  ${position1!!.x}")
                    }
            )

            BasicText(
                text = "语音配置",
                style = TextStyle(
                            fontWeight = FontWeight.Bold,
                    fontSize = size,
                    color = if(currentTab.value=="语音配置") MYBLACK else MYBLACK_Percent60 ,
                ),
                modifier = Modifier
                    .clickable(
                        indication = null,  // Removes the ripple effect
                        interactionSource = remember { MutableInteractionSource() }  // Keeps track of interactions
                    ) {
                        showLeft.value = false;
                        showRight.value = false;
                        centerX.intValue = position2
                        largeX.intValue= 0
                        currentTab.value = "语音配置"
                    }
                    .onGloballyPositioned {
                        position2 = (it.positionInParent().x +  it.size.width/2).roundToInt()
//                        println(TAG+" Font size is position2:  ${position2!!.x}")
                    }
            )

            BasicText(
                text = "客户管理",
                style = TextStyle(
                            fontWeight = FontWeight.Bold,
                    fontSize = size,
                    color = if(currentTab.value=="客户管理") MYBLACK else MYBLACK_Percent60 ,
                ),
                modifier = Modifier
                    .clickable(
                        indication = null,  // Removes the ripple effect
                        interactionSource = remember { MutableInteractionSource() }  // Keeps track of interactions
                    ) {
                        showLeft.value = false;
                        showRight.value = false;
                        centerX.intValue = position3
                        largeX.intValue= 0
                        currentTab.value = "客户管理"
                    }
                    .onGloballyPositioned {
                        position3 = (it.positionInParent().x +  it.size.width/2).roundToInt()
                    }
            )

            BasicText(
                text = "渲染钱包充值管理",
                style = TextStyle(
                            fontWeight = FontWeight.Bold,
                    fontSize = size,
                            color = if(currentTab.value=="渲染钱包充值管理") MYBLACK else MYBLACK_Percent60 ,
                ),
                modifier = Modifier
                    .clickable(
                        indication = null,  // Removes the ripple effect
                        interactionSource = remember { MutableInteractionSource() }  // Keeps track of interactions
                    ) {
                        showLeft.value = false;
                        showRight.value = false;
                        largeX.intValue= position4
                        centerX.intValue = 0
                        currentTab.value = "渲染钱包充值管理"
                    }
                    .onGloballyPositioned {
                             position4 = (it.positionInParent().x +  it.size.width/2).roundToInt()
                    }
            )

            BasicText(
                text = "申请管理",
                style = TextStyle(
                            fontWeight = FontWeight.Bold,
                    fontSize = size,
                            color = if(currentTab.value=="申请管理") MYBLACK else MYBLACK_Percent60 ,
                ),
                modifier = Modifier.clickable(
                    indication = null,  // Removes the ripple effect
                    interactionSource = remember { MutableInteractionSource() }  // Keeps track of interactions
                ) {
                    showLeft.value=false;
                    showRight.value=false;
                    centerX.intValue = position5
                    largeX.intValue= 0
                    currentTab.value = "申请管理"
                } .onGloballyPositioned {
                    position5 = (it.positionInParent().x +  it.size.width/2).roundToInt()
                }
            )

            BasicText(
                text = "员工管理",
                style = TextStyle(
                    fontWeight = FontWeight.Bold,
                    fontSize = size,
                    color = if(currentTab.value=="员工管理") MYBLACK else MYBLACK_Percent60 ,
                ),
                modifier = Modifier.clickable(
                    indication = null,  // Removes the ripple effect
                    interactionSource = remember { MutableInteractionSource() }  // Keeps track of interactions
                ) {
                    showLeft.value=false;
                    showRight.value=true;
                    centerX.intValue = 0
                    largeX.intValue= 0
                    currentTab.value = "员工管理"
                }
                    .onGloballyPositioned {
                        position6 = (it.positionInParent().x +  it.size.width/2).roundToInt()
                    }
            )

        }
    }

//悬浮窗在layout文件里面.
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding  =  ContentMain4Binding.inflate(layoutInflater)
        setContentView(binding.root)

    enableSpClick(){
        finish()
        startActivity<ShangPinKuActivity>()
    }

    enableInfoClick(){
    }

    enableFaClick {
        finish()
        startActivity<FangAnKuActivity>()
    }

    enableHomeClick(){
        finish()
    }

        val composeView: ComposeView =  binding.tab
        // Set Compose content
        composeView.setContent {
//            SpToPxExample()
//            if(1==1)  return@setContent
            val showLeft = remember { mutableStateOf(true)}
            val showRight = remember { mutableStateOf(false)}
            val centerX = remember { mutableIntStateOf(0) }
            val largeX = remember { mutableIntStateOf(0) }
            val currentTab = remember  { mutableStateOf(TAB1) }

            val scale by animateDpAsState(targetValue = 100.dp,animationSpec =
            tween(durationMillis = 2000), label = ""
            )

            val tabs = remember { mutableStateOf(false)}

            Column {
                Spacer(modifier = Modifier.height(30.px))

                Row {
                    Spacer(modifier = Modifier.width(30.px))
                    //tab开始
            Box(
                modifier = Modifier
                    .size(1600.px, 90.px)
                    .background(
                        Color.White.copy(alpha = 0.4f), shape = RoundedCornerShape(40.px,40.px,0.px,0.px)
                    ),
                contentAlignment = Alignment.Center
            ) {
                if(showLeft.value){
                    Image(
                        modifier = Modifier.align(Alignment.TopStart),
                        painter = painterResource(id = R.drawable.tab_left), // 替换为你的图片资源ID
                        contentDescription = null,
                        contentScale = ContentScale.Crop, // 使图片填充整个背景
                    )
                }
                 else
                if(showRight.value){
                    Image(
                        modifier = Modifier.align(Alignment.BottomEnd),
                        painter = painterResource(id = R.drawable.tab_right), // 替换为你的图片资源ID
                        contentDescription = null,
                        contentScale = ContentScale.Crop, // 使图片填充整个背景
                    )
                }
                else
                if(centerX.intValue != 0) {
                    if(debug) {
                        Surface(
                            modifier = Modifier
                                .align(Alignment.TopStart)
                                .size(270.px, 90.px)
                                .offset(x = (centerX.intValue + 50 - 135).px),
                            color = Color.Blue
                        ) {
                        }

                        Surface(
                            modifier = Modifier
                                .align(Alignment.TopStart)
                                .size(1.px, 90.px)
                                .offset(x = (centerX.intValue + 50).px), color = Color.Yellow
                        ) {
                        }
                    }

                    Image(
                        modifier = Modifier
                            .align(Alignment.TopStart)
                            .offset(x = (centerX.intValue-135+50).px),
                        painter = painterResource(id = R.drawable.tab_center), // 替换为你的图片资源ID
                        contentDescription = null,
                        contentScale = ContentScale.Crop, // 使图片填充整个背景
                    )
                }
                else
                if(largeX.intValue != 0) {
//                    if(debug) {
//                        Surface(
//                            modifier = Modifier
//                                .align(Alignment.TopStart)
//                                .size(270.px, 90.px)
//                                .offset(x = (centerX.intValue + 50 - 135).px),
//                            color = Color.Blue
//                        ) {
//                        }
//
//                        Surface(
//                            modifier = Modifier
//                                .align(Alignment.TopStart)
//                                .size(1.px, 90.px)
//                                .offset(x = (centerX.intValue + 50).px), color = Color.Yellow
//                        ) {
//                        }
//                    }

                    Image(
                        modifier = Modifier
                            .align(Alignment.TopStart)
                            .offset(x = (largeX.intValue-185+50).px),
                        painter = painterResource(id = R.drawable.tab_large), // 替换为你的图片资源ID
                        contentDescription = null,
                        contentScale = ContentScale.Crop, // 使图片填充整个背景
                    )
                }
                //                tab标题信息
                TabTitle(showLeft,showRight,centerX,largeX,currentTab)
                }
                }
            }

            when(currentTab.value){
                TAB1 -> {
                    FragmentContainer(SystemMessageFragment2())
                }
                tab2 -> {
                    FragmentContainer(RobotInfoFragment())
                }
                tab3 -> {
                    FragmentContainer(QAFragment())
                }
                tab4 -> {
                    FragmentContainer(CustomersFragment())
                }
                tab5 -> {
                    FragmentContainer(WalletFragment())
                }
                tab6,tab7 -> {
                    FragmentContainer(RobotInfoFragment7())
                }

               else -> {
               }
            }
//            if(currentTab.value==""){
//                FragmentContainer(PlayerFragment())
//
//
//            }
            }
    }

    @Composable
    fun FragmentContainer(fragment: Fragment) {
        val fragmentTransaction = supportFragmentManager.beginTransaction()
        fragmentTransaction.replace(R.id.fragment_container, fragment)
        fragmentTransaction.commit()
    }

    companion object{
        val debug= 1==2;
        val  size= 30.mysp
        val  TAG="SysMessActivity"
        val  TAB1="系统信息"
        val  tab2="机器人信息"
        val  tab3="语音配置"
        val  tab4="客户管理"
        val  tab5="渲染钱包充值管理"
        val  tab6="申请管理"
        val  tab7="员工管理"
    }
}

